home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / finger.c < prev    next >
C/C++ Source or Header  |  1991-06-04  |  6KB  |  274 lines

  1. /* @(#) $Header: finger.c,v 1.8 91/06/04 11:33:50 deyke Exp $ */
  2.  
  3. /*
  4.  *
  5.  *      Finger support...
  6.  *
  7.  *      Finger client routines.  Written by Michael T. Horne - KA7AXD.
  8.  *      Copyright 1988 by Michael T. Horne, All Rights Reserved.
  9.  *      Permission granted for non-commercial use and copying, provided
  10.  *      that this notice is retained.
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include "config.h"
  16. #include "global.h"
  17. #include "mbuf.h"
  18. #include "timer.h"
  19. #include "internet.h"
  20. #include "icmp.h"
  21. #include "netuser.h"
  22. #include "tcp.h"
  23. #include "ftp.h"
  24. #include "telnet.h"
  25. #include "iface.h"
  26. #include "ax25.h"
  27. /* #include "lapb.h" */
  28. #include "finger.h"
  29. #include "session.h"
  30. /* #include "nr4.h" */
  31.  
  32. static struct finger *alloc_finger __ARGS((void));
  33. static int free_finger __ARGS((struct finger *finger));
  34. static void f_state __ARGS((struct tcb *tcb, int old, int new));
  35.  
  36. /*
  37.  *
  38.  *      Open up a socket to a remote (or the local) host on its finger port.
  39.  *
  40.  */
  41.  
  42. int
  43. dofinger(argc,argv,p)
  44. int     argc;
  45. char    *argv[];
  46. void *p;
  47. {
  48.     struct session  *s;
  49.     struct tcb      *tcb;
  50.     struct socket   lsocket,
  51.             fsocket;
  52.     struct finger   *finger;
  53.     char            *host;
  54.  
  55.     if (argc < 2) {
  56.         printf("usage: %s [user | user@host | @host]\n", argv[0]);
  57.         return(1);
  58.     }
  59.  
  60.     lsocket.address = INADDR_ANY;
  61.     lsocket.port = Lport++;
  62.  
  63. /*
  64.  *      Extract user/host information.  It can be of the form:
  65.  *
  66.  *      finger user,                    # finger local user
  67.  *      finger user@host,               # finger remote user
  68.  *      finger @host                    # finger host (give system status)
  69.  *
  70.  */
  71.     if ((finger = alloc_finger()) == NULLFING)
  72.         return(1);
  73.  
  74.     if ((host = strrchr(argv[1], '@')) == NULL) {
  75.         fsocket.address = Loopback.addr;/* no host, use local */
  76.         if ((finger->user = malloc(strlen(argv[1]) + 3)) == NULL) {
  77.             free_finger(finger);
  78.             return(1);
  79.         }
  80.         strcpy(finger->user, argv[1]);
  81.         strcat(finger->user, "\015\012");
  82.     }
  83.     else {
  84.         *host++ = '\0';         /* null terminate user name */
  85.         if (*host == '\0') {    /* must specify host */
  86.             printf("%s: no host specified\n", argv[0]);
  87.             printf("usage: %s [user | user@host | @host]\n",
  88.                 argv[0]);
  89.             free_finger(finger);
  90.             return(1);
  91.         }
  92.         if ((fsocket.address = resolve(host)) == 0) {
  93.             printf("%s: ", argv[0]);
  94.             printf(Badhost, host);
  95.             free_finger(finger);
  96.             return(1);
  97.         }
  98.         if ((finger->user = malloc(strlen(argv[1])+3))==NULL) {
  99.             free_finger(finger);
  100.             return 1;
  101.         }
  102.         strcpy(finger->user, argv[1]);
  103.         strcat(finger->user, "\015\012");
  104.     }
  105.  
  106.     fsocket.port = FINGER_PORT;             /* use finger wnp */
  107.  
  108.     /* Allocate a session descriptor */
  109.     if ((s = newsession()) == NULLSESSION){
  110.         printf("Too many sessions\n");
  111.         free_finger(finger);
  112.         return 1;
  113.     }
  114.     Current = s;
  115.     s->cb.finger = finger;
  116.     finger->session = s;
  117.  
  118.     if (!host)                              /* if no host specified */
  119.         host = Hostname;                /* use local host name */
  120.     if ((s->name = malloc(strlen(host)+1)) != NULLCHAR)
  121.         strcpy(s->name, host);
  122.  
  123.     s->type = FINGER;
  124.     s->parse = 0;
  125.  
  126.     tcb = open_tcp(&lsocket, &fsocket, TCP_ACTIVE, 0,
  127.      fingcli_rcv, (void (*)()) 0, f_state, 0, (int) finger);
  128.  
  129.     finger->tcb = tcb;
  130.     go(argc, argv, p);
  131.     return 0;
  132. }
  133.  
  134. /*
  135.  *      Allocate a finger structure for the new session
  136.  */
  137. static struct finger *
  138. alloc_finger()
  139. {
  140.     struct finger *tmp;
  141.  
  142.     if ((tmp = (struct finger *) malloc(sizeof(struct finger))) == NULLFING)
  143.         return(NULLFING);
  144.     tmp->session = NULLSESSION;
  145.     tmp->user = (char *) NULL;
  146.     return(tmp);
  147. }
  148.  
  149. /*
  150.  *      Free a finger structure
  151.  */
  152. static int
  153. free_finger(finger)
  154. struct finger *finger;
  155. {
  156.     if (finger != NULLFING) {
  157.         if (finger->session != NULLSESSION)
  158.             freesession(finger->session);
  159.         if (finger->user != (char *) NULL)
  160.             free(finger->user);
  161.         free(finger);
  162.     }
  163.     return 0;
  164. }
  165.  
  166. /* Finger receiver upcall routine */
  167. void
  168. fingcli_rcv(tcb, cnt)
  169. register struct tcb     *tcb;
  170. int16                   cnt;
  171. {
  172.     struct mbuf     *bp;
  173.     char            *buf;
  174.  
  175.     /* Make sure it's a valid finger session */
  176.     if ((struct finger *) tcb->user == NULLFING) {
  177.         return;
  178.     }
  179.  
  180.     /* Hold output if we're not the current session */
  181.     if (Mode != CONV_MODE || Current == NULLSESSION
  182.         || Current->type != FINGER)
  183.         return;
  184.  
  185.     /*
  186.      *      We process the incoming data stream and make sure it
  187.      *      meets our requirments.  A check is made for control-Zs
  188.      *      since these characters lock up DoubleDos when sent to
  189.      *      the console (don't you just love it...).
  190.      */
  191.  
  192.     if (recv_tcp(tcb, &bp, cnt) > 0)
  193.         while (bp != NULLBUF) {
  194.             buf = bp->data;
  195.             while(bp->cnt--) {
  196.                 switch(*buf) {
  197.                     case '\012':    /* ignore LF */
  198.                     case '\032':    /* NO ^Z's! */
  199.                         break;
  200.                     case '\015':
  201.                         fputc('\015', stdout);
  202.                         fputc('\012', stdout);
  203.                         break;
  204.                     default:
  205.                         fputc(*buf, stdout);
  206.                         break;
  207.                 }
  208.                 buf++;
  209.             }
  210.             bp = free_mbuf(bp);
  211.         }
  212. }
  213.  
  214. /* State change upcall routine */
  215. static void
  216. f_state(tcb,old,new)
  217. register struct tcb     *tcb;
  218. char                    old,            /* old state */
  219.             new;            /* new state */
  220. {
  221.     struct finger   *finger;
  222.     char            notify = 0;
  223.     struct mbuf     *bp;
  224.  
  225.     finger = (struct finger *)tcb->user;
  226.  
  227.     if(Current != NULLSESSION && Current->type == FINGER)
  228.         notify = 1;
  229.  
  230.     switch(new){
  231.  
  232.     case TCP_CLOSE_WAIT:
  233.         if(notify)
  234.             printf("%s\n",Tcpstates[new]);
  235.         close_tcp(tcb);
  236.         break;
  237.  
  238.     case TCP_CLOSED:    /* finish up */
  239.         if(notify) {
  240.             printf("%s (%s", Tcpstates[new], Tcpreasons[tcb->reason]);
  241.             if (tcb->reason == NETWORK){
  242.                 switch(tcb->type){
  243.                 case ICMP_DEST_UNREACH:
  244.                     printf(": %s unreachable",Unreach[tcb->code]);
  245.                     break;
  246.                 case ICMP_TIME_EXCEED:
  247.                     printf(": %s time exceeded",Exceed[tcb->code]);
  248.                     break;
  249.                 }
  250.             }
  251.             printf(")\n");
  252.             cmdmode();
  253.         }
  254.         if(finger != NULLFING)
  255.             free_finger(finger);
  256.         del_tcp(tcb);
  257.         break;
  258.     case TCP_ESTABLISHED:
  259.         if (notify) {
  260.             printf("%s\n",Tcpstates[new]);
  261.         }
  262.         printf("[%s]\n", Current->name);
  263.         bp = qdata(finger->user, (int16) strlen(finger->user));
  264.         send_tcp(tcb, bp);
  265.         break;
  266.  
  267.     default:
  268.         if(notify)
  269.             printf("%s\n",Tcpstates[new]);
  270.         break;
  271.     }
  272. }
  273.  
  274.